// index.js
// 获取应用实例
import {post} from "../../asserts/js/request";
import {initStudentInfo, initTeacherInfo} from "../../asserts/js/common/initInof";
var app = getApp();
Page({
  data: {
    className:"",
    stuName:"",
    classId: null,
    optSwitch: false,
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    top: app.globalData.bottom + 'px'
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  showJoinClass: function(){
    this.setData({
      optSwitch: true
    })
  },
  showCommonPage: function(){
    this.setData({
      optSwitch: false
    })
  },
  inputName: function(e){
    this.setData({
      stuName: e.detail.value.trim()
    })
  },
  submitRequire: function(){
    let this_= this;
    console.log(this.data)
    if(this.data.stuName.trim() == ""){
      wx.showToast({
        title: '请输入您的姓名',
        icon: 'error'
      })
      return;
    }else{
      post("studentSignInClass",{
        openId: app.globalData.openId,
        nickName: this.data.stuName,
        classId: this.data.classId
      }).then(data => {
        console.log(data);
        wx.showToast({
          title: '已提交申请，等待审核',
          icon: "none"
        })

        this_.showCommonPage();
      });
    }
  },
  onLoad(query) {
    // scene 需要使用 decodeURIComponent 才能获取到生成二维码时传入的 scene
    const scene = decodeURIComponent(query.scene)
    console.log("扫描二维码信息"+ scene);
    let this_= this;

    if(scene != null && scene > 10000){ 
      console.log("跳转特殊的流程");

      this.setData({
        classId: scene
      });
      post("queryClassById", {
        classId: scene,
      }).then(data => {
        this_.setData({
          className: data.classInfo.className
        });
        console.log("待加入班级名称"+ data.classInfo.className)
        this_.showJoinClass();
      });  
    }

    wx.getSetting({
      withSubscriptions: true,
      success: data => {
        if(data.authSetting['scope.userInfo'] == true) {
          wx.getUserInfo({
            success: data => {
              console.log("获取用户信息");
              app.globalData.userInfo = data.userInfo;
              // console.log(data);
            }
          })
        }
      }
    })
  },
  getUserInfoStu(e) {
    console.log("get user info begin")
    let this_ = this;

    wx.getUserProfile({
      desc:"用于完善会员资料",
      success: (res) =>{
        app.globalData.userInfo = res.userInfo;
        this.updateUserInfo();
        initStudentInfo()
        wx.redirectTo({
          url:"/pages/student/mine/center/mine"
        })
  
      },
      fail:()=>{
        console.log("failed")
        wx.showToast({
          title: '您需要授权教练才能为您进行一对一指导。',
          icon: "none"
        })
      }
    })
  },
  getUserInfoTea(e) {
    let this_ = this;
    if(e.detail.errMsg == "getUserInfo:ok") {
      app.globalData.userInfo = e.detail.userInfo;
      this.updateUserInfo();
    }
    initTeacherInfo("testteacher","123456").then(data => {
      this_.redirect("tea");
    }).then(err => {
      console.log(err)
    })
  },
  redirect: function (type) {
    var url = "";
    if (type == "tea") {
      url = "/pages/teacher/mine/center/mine"
    }else {
      url = "/pages/student/mine/center/mine";
    }
    wx.redirectTo({
      url: url,
    })
  },
  updateUserInfo: function() {
    var param = {

    }
    Object.assign(param, app.globalData.userInfo)
    param.openId = app.globalData.openId
    post("wxSetUserInfo", param).then(data => {
      console.log("更新了数据")
    })
  },
  gotoTest: function(){
    wx.navigateTo({
      url: '/pages/teacher/myclass/teacherStuCourse/teacherStuCourse',
    })
  }
})
